﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using Microsoft.Practices.Unity;
using ScrumTable.BL.DM.DataManagement;
using ScrumTable.UI.ViewModel.Data;
using ScrumTable.UI.ViewModel.Extension; 
using System.Linq;
#endregion

namespace ScrumTable.UI.ViewModel.View
{
    /// <summary>
    /// View model for the Project Planning Scrum Poker View
    /// </summary>
    public class ProjectPlanningIterationsViewModel : ViewModelBase
    {

        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        private ScrumViewModelData _scrumData;
        
        private ObservableCollection<IScrumItemViewModel<DomainBase>> _projectItems;

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------  

        /// <summary>
        /// Gets or sets the scrum data.
        /// </summary>
        /// <value>The scrum data.</value>
        [Dependency]
        public ScrumViewModelData ScrumData
        {
            get
            {
                return _scrumData;
            }
            set
            {
                _scrumData = value;
                ScrumData.DataContext.ElementChanged += OnContextElementChanged;
                ScrumData.DataContext.CollectionChanged += OnContextDataCollectionChanged;
                ScrumData.CurrentProject.Bugs.CollectionChanged += new DomainCollectionChangedEventHandler(OnItemCollectionChanged);
                ScrumData.CurrentProject.Stories.CollectionChanged += new DomainCollectionChangedEventHandler(OnItemCollectionChanged);
            }
        }

       

        /// <summary>
        /// Gets the name of the current project.
        /// </summary>
        /// <value>The name of the current project.</value>
        public string ProjectName
        {
            get
            {
                if (ScrumData.CurrentProject != null)
                    return ScrumData.CurrentProject.Name;
                return "";
            }
        }

        private ObservableCollection<IterationViewModel> _iterations;
        /// <summary>
        /// Gets the iterations of the current project.
        /// </summary>
        /// <value>The iterations of the current project.</value>
        public ObservableCollection<IterationViewModel> Iterations
        {
            get
            {
                var project = ScrumData.CurrentProject;
                if (project != null && _iterations == null)
                {
                    _iterations = new ObservableCollection<IterationViewModel>((from iteration in project.Iterations
                                                                                select new IterationViewModel(iteration, ScrumData)).ToList());
                    UpdateIterationStoryPoints();
                }
                return _iterations;
            }
        }
         

        /// <summary>
        /// Gets the user stories of the iteration.
        /// </summary>
        public ObservableCollection<IScrumItemViewModel<DomainBase>> ProjectItems
        {
            get
            {
                if (ScrumData.CurrentIteration != null)
                {
                    var stories = from x in ScrumData.CurrentProject.Stories
                                  where ScrumData.ContainsFilter(x)
                                  select (IScrumItemViewModel<DomainBase>)new UserStoryViewModel(x);

                    if (_scrumData.ConfigManager.Config.IsBugUserStory)
                    {
                        var bug1 = from x in ScrumData.CurrentProject.Bugs
                                   where ScrumData.ContainsFilter(x)
                                   select (IScrumItemViewModel<DomainBase>)new BugViewModel(x);
                        stories = stories.Union(bug1);
                    }

                    return ViewModelItemObservableCollectionExtensions.CombineList(stories, ref _projectItems);
                }
                return _projectItems;
            }
        }

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //-------------------------------------------------------------------- 

        /// <summary>
        /// Releases managed (c# or .net) resources.
        /// </summary>
        protected override void ReleaseManagedResources()
        { 
            ScrumData.DataContext.ElementChanged -= OnContextElementChanged;
            ScrumData.DataContext.CollectionChanged -= OnContextDataCollectionChanged;

            if (_iterations != null)
            {
                foreach (var iteration in _iterations)
                {
                    iteration.Dispose();
                }
                _iterations.Clear();
            }
             

            base.ReleaseManagedResources();
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        private void UpdateIterationStoryPoints()
        {
            foreach (var iteration in Iterations)
            {
                iteration.UpdateTotalStoryPoints();
            }
        }

      
        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------
        private void OnContextElementChanged(object sender, PropertyChangedEventArgs e)
        {
            UpdateIterationStoryPoints();
            var args = e as DomainChangedEventArgs;
            if (args != null && args.ContainsPropertyName("Ranking"))
            {
                SendPropertyChanged("ProjectItems");
                foreach( var it in Iterations)
                {
                    it.Resort();
                }
            }
        }

       

        private void OnContextDataCollectionChanged(object sender, DomainCollectionChangedEventArgs e)
        {
            UpdateIterationStoryPoints();
        }

        private void OnItemCollectionChanged(object sender, DomainCollectionChangedEventArgs e)
        {
            SendPropertyChanged("ProjectItems");
        }


        #endregion
    }


}